<template>
  <div ref="echartDom"></div>
</template>

<script setup>
import {ref, defineProps, onMounted, onUnmounted, inject, shallowRef, defineExpose} from 'vue'
import {pxToRem} from "@/utils/index";

const props = defineProps({
  pieData: {
    type: Array,
    default: () => []
  },
  dialogData: {
    type: Object,
    default: () => ({
      fontSizeTimes: 1
    })
  }
});

const echartDom = ref(null);
const myChart = shallowRef(null);
const echarts = inject('echarts');

const tempDom = (params) => {
  let tempDom = ''
  const tempTbClass = params?.data?.tb ? 'triangle-reverse' : ''
  const tempHbClass = params?.data?.hb ? 'triangle-reverse' : ''
  const tempTbColor = params?.data?.tb ? '#01FEFF' : '#E13535'
  const tempHbColor = params?.data?.hb ? '#01FEFF' : '#E13535'

  switch(props?.pieData?.type) {
    case 1:
      tempDom = '<br/>' + `<div style="display: flex; align-items: center"><div class="triangle ${tempTbClass}"></div>&nbsp;<span style="color: ${tempTbColor}">${params.data?.tb}%</span>` + `&nbsp;同比</div>`
                        + `<div style="display: flex; align-items: center"><div class="triangle ${tempHbClass}"></div>&nbsp;<span style="color: ${tempHbColor}">${params.data?.hb}%</span>` + `&nbsp;环比</div>`
      break
    case 2:
      tempDom = '<br/>' + `<div style="display: flex; align-items: center"><div class="triangle ${tempTbClass}"></div>&nbsp;<span style="color: ${tempTbColor}">${params.data?.tb}%</span>` + `&nbsp;同比</div>`
      break
    default:
      tempDom = ''
      break
  }
  return tempDom
}

const initEchart = () => {
  if (echartDom.value) {
    myChart.value = echarts.init(echartDom.value);
    let option = {
      tooltip: {
        show: true,
        formatter: (params) => {
          return (
              `${params.name}：${params.value}` + '<br/>' +
              `占比：${params.percent}%` + tempDom(params)
          );
        },
        position: (point) => {
          return [point[0], point[1]]
        }
      },
      legend: {
        left: 'center',
        itemHeight: pxToRem(12 * props?.dialogData?.fontSizeTimes),
        itemWidth: pxToRem(12 * props?.dialogData?.fontSizeTimes),
        y: `${props?.dialogData?.fontSizeTimes === 1 ? 15 : 5}%`,
        padding: [0, 0, 0],
        textStyle: {
          color: "#fff",
          fontSize: pxToRem(12 * props?.dialogData?.fontSizeTimes),
          rich: {},
          padding: [0, 0, pxToRem(-3 * props?.dialogData?.fontSizeTimes), 0],
        },
        itemGap: pxToRem(10 * props?.dialogData?.fontSizeTimes)
      },
      series: [
        {
          type: 'pie',
          radius: `${props?.dialogData?.fontSizeTimes === 1 ? 50 : 70 }%`,
          center: ['50%', '60%'],
          label: {
            show: false,
          },
          data: props?.pieData?.seriesData,
          emphasis: {
            itemStyle: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          }
        }
      ]
    }
    option && myChart.value.setOption(option, true);
  }
}

const updateChart = () => {
  if (myChart.value) {
    myChart.value.dispose(); // 销毁旧图表实例
  }
  initEchart();
};

defineExpose({
  updateChart
})

onMounted(() => {
  initEchart();
  window.addEventListener("resize", updateChart);
})

onUnmounted(() => {
  window.removeEventListener("resize", updateChart);
})
</script>

<style scoped lang="less">
.h276 {
  height: 276px;
}
.dialog-h276 {
  width: 1120px;
  height: 500px;
}

:deep(.triangle) {
  width: 0;
  height: 0;
  border: 5px solid transparent;
  border-top: 5px solid #E13535;
  border-bottom: 0;
  display: inline-flex;
  align-items: center;
  margin-right: 5px;
}

:deep(.triangle-reverse) {
  transform: rotate(180deg);
  border-top: 5px solid #01FEFF;
}
</style>